<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Index extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('index_model');
        $this->load->model('bbs_index_model');
        $this->assign("module", "index");
    }

    function index($type,$value) {
        $this->left();
        $this->right();
        if($type == 'tag'){
            $tag = $this->db->select("name")->from("bbs_article_tags")->where(array('id'=>$value))->order_by("id desc")->get()->row_array();
            if(!$tag){//不存在标签
                $this->assign("error", "标签不存在或已被系统管理员删除");
                $this->display('deleted/index.html');exit;
            }
            $tag_name = $tag['name'];
            $tag_flag = true;
        }elseif($type == 'cat'){
            $cat = $this->db->select("name")->from("bbs_article_category")->where(array('id'=>$value))->order_by("id desc")->get()->row_array();
            if(!$cat){//不存在板块
                $this->assign("error", "板块不存在或已被系统管理员删除");
                $this->display('deleted/index.html');exit;
            }
        }else{
            if($type){
                $parent_id = decrypt_user_id($type);
                $parent_user = $this->users_model->get_user_by_uid(intval($parent_id));
                if($parent_user){
                    if($parent_user['status'] == 0){
                        web_redirect('error/index');
                    }else{
                        $this->session->set_userdata('parent_id',$parent_id);
                    }
                }
                $type = "";
            }
        }
        if(!$type){
            
            $where = "a.hot = 1 and a.status = 1 and a.show = 1 order by a.sort desc";
            $sqlCount = "select a.* from tan_bbs_article a where ".$where;//构造查询结果总数
            $sqlData = "select a.id,a.authorid,a.title,a.summary,a.cat_id,a.thumb,a.tags_name,a.readnum,a.commentnum,a.likes,a.status,a.create_time,c.name cat_name,u.nickname from tan_bbs_article a left join tan_bbs_article_category c on c.id = a.cat_id left join tan_users_info u on u.user_id = a.authorid where ".$where." limit 12";//构造查询结果记录
            $totalCount = $this->db->query($sqlCount)->num_rows();
            
            $list = $this->memcached->get("www_hot_article");
            if($list){
                $list = unserialize($list);
                foreach ($list as $key=>$val){
                    $art_ = $this->db->query("select readnum,commentnum,likes from tan_bbs_article where id = $val[id] order by id desc limit 1")->row_array();
                    $list[$key]['readnum'] = $art_['readnum'];
                    $list[$key]['commentnum'] = $art_['commentnum'];
                    $list[$key]['likes'] = $art_['likes'];
                    $list[$key]['summary'] = str_replace(" ","",  mb_strcut($val["summary"], 0, 350, "utf-8"));
                    $list[$key]['detail_url'] = web_url("p/".encrypt_article_id($val['id']));
                    $list[$key]['cat_url'] = web_url("p/cat-".$val['cat_id']);
                    $list[$key]['space_url'] = web_url("u/".encrypt_user_id($val['authorid']));
                    $list[$key]['article_id'] = encrypt_article_id($val['id']);
                    $list[$key]['auid_id'] = encrypt_user_id($val['authorid']);
                    $tags_str = "";
                    if($val['tags_name']){
                        $tags_arr = explode(",", $val['tags_name']);
                        foreach ($tags_arr as $v){
                            $v = explode("_", $v);
                            $tags_str .= "<a href='".web_url("p/tag-".$v[1])."' ".($tag['id'] == $v[1] ? "class='on'" : "").">$v[0]</a>";
                        }
                    }
                    $is_like = $this->bbs_userinfo ? (($this->db->query("select * from tan_bbs_like where article_id = $val[id] and user_id = ".$this->bbs_userinfo['user_id'])->num_rows()) ? 1 : 0) : 0;
                    $list[$key]['tag_str'] = $tags_str;
                    $list[$key]['is_like'] = $is_like;
                }
                
            }else{
                $list = $this->db->query($sqlData)->result_array();
                foreach ($list as $key=>$val){
                    $list[$key]['detail_url'] = web_url("p/".encrypt_article_id($val['id']));
                    $list[$key]['cat_url'] = web_url("p/cat-".$val['cat_id']);
                    $list[$key]['space_url'] = web_url("u/".encrypt_user_id($val['authorid']));
                    $list[$key]['article_id'] = encrypt_article_id($val['id']);
                    $list[$key]['auid_id'] = encrypt_user_id($val['authorid']);
                    $list[$key]['summary'] = str_replace(" ","",  mb_strcut($val["summary"], 0, 350, "utf-8"));

                    $tags_str = "";
                    if($val['tags_name']){
                        $tags_arr = explode(",", $val['tags_name']);
                        foreach ($tags_arr as $v){
                            $v = explode("_", $v);
                            $tags_str .= "<a href='".web_url("p/tag-".$v[1])."' ".($tag['id'] == $v[1] ? "class='on'" : "").">$v[0]</a>";
                        }
                    }
                    $is_like = $this->bbs_userinfo ? (($this->db->query("select * from tan_bbs_like where article_id = $val[id] and user_id = ".$this->bbs_userinfo['user_id'])->num_rows()) ? 1 : 0) : 0;
                    $list[$key]['tag_str'] = $tags_str;
                    $list[$key]['is_like'] = $is_like;
                }
            }
            $this->assign("list", $list);
            $this->assign("totalCount", $totalCount);
        }
        //获取banner图片
        $banners = $this->db->query("select banner_img,banner_link from tan_banner where status = 1 and type = 4 order by sort desc")->result_array();
        
        $this->notice_msg();
        
        $this->assign("banners", $banners);
        $this->assign("type", $type ? $type : 0);
        $this->assign("value", $value ? $value : 0);
        $this->assign("tag_name", $tag_name ? $tag_name : 0);
        $this->assign("tag_flag", $tag_flag);
        $this->assign("cates", $this->forummenu);
        $this->display('index/index.html');
    }
    
    //中间轮播消息
    private function notice_msg(){
        $where_in = "'event_reg','event_focus_author'";
        $where_in .= ",'event_collect_article','event_reply_article','event_like_article','event_comment_article','event_reward_article','event_add_article'";
        $where_in .= ",'event_comment_wenda','event_reply_wenda','event_like_answer','event_care_answer','event_best_answer','event_add_wenda'";
        $sys_notices = $this->db->query("select * from tan_bbs_notice where  event_type in($where_in) order by id desc limit 20")->result_array();
        $sys_notices = $this->set_system_notice($sys_notices);
        $this->assign("sys_notices", $sys_notices);
    }
    
    //首页右侧信息
    private function right(){
        //默认热门文章
        
    }
    
    private function left(){
        //公告
        $reports = $this->db->query("select c.id,c.title,c.cate_id,c.create_time from tan_content c where c.cate_id = 21 and c.status = 1 order by c.create_time desc limit 5")->result_array();
        foreach ($reports as $key=>$val){
            $reports[$key]['create_time'] = date("m月d日",$val['create_time']);
            $reports[$key]['detail_url'] = finance_base_url("news/21/".$val['id']);
            $reports[$key]['title'] = mb_strcut($val['title'], 0,45,"utf-8")."...";
        }
        $this->assign("reports", $reports);
        
        //问答
        $wendas = $this->db->query("select id,title,answernum from tan_bbs_wenda where status = 1 and hot =1 and is_show=1 order by sort desc limit 6")->result_array();
        foreach($wendas as $key=>$val){
            $wendas[$key]['title'] = mb_strcut($val['title'], 0,45,"utf-8")."...";
            $wendas[$key]['detail_url'] = site_url("wenda/".encrypt_wenda_id($val['id']));
            $wendas[$key]['num'] = $val['answernum'];
        }
        $this->assign("wendas", $wendas);
        
        //推荐认证用户
        $recommend_users = $this->db->query("select v.user_id,i.nickname,i.signature,i.head_img from tan_bbs_user_verified v left join tan_users_info i on i.user_id = v.user_id  where v.status = 1 and v.is_recommend = 1 order by v.check_time desc limit 5")->result_array();
        foreach ($recommend_users as $key=>$val){
            $recommend_users[$key]["space_url"] = web_url("u/".encrypt_user_id($val['user_id']));
        }
        $this->assign("recommend_users", $recommend_users);
    }
    
    
    function report(){
        if($_POST){
            $target_id = isset($_POST['target_id']) ? trim($_POST['target_id']) : 0;
            $target_type = isset($_POST['target_type']) ? trim($_POST['target_type']) : 0;
            $target_content = isset($_POST['target_content']) ? trim($_POST['target_content']) : "";
            
            if($target_type != 1 && $target_type != 2 && $target_type != 3){
                $errmes['reqStatus'] = false;
                $errmes['errorMsg'] = "非法参数";
                echo JSON($errmes);exit;
            }
            
            $data = array();
            $data['target_id'] = $target_id;
            $data['target_type'] = $target_type;
            $data['target_content'] = $target_content;
            $data['create_time'] = time();
            
            $this->db->insert("bbs_report_list",$data);
            if($this->db->affected_rows()){
                $errmes['reqStatus'] = true;
                $errmes['errorMsg'] = "举报成功";
                echo JSON($errmes);exit;
            }else{
                $errmes['reqStatus'] = false;
                $errmes['errorMsg'] = "操作失败，请稍后重试";
                echo JSON($errmes);exit;
            }
            
        }
    }
    function push(){
        $this->load->library('my_getui');
        
        $push = array();
        $push['type'] = "award_notice_type";
        $push['title'] = "奖励通知";
        $push['content'] = "您收到一条奖励通知";
        $push['message_id'] = 16;
        $Push = new My_getui();
        $Push->pushMessageToSingle(2026001,$push);
    }
    
    function tt(){
        insert_award_notice(array('user_id'=>10001,'title'=>'加息通知','content'=>"你好呀，哈哈",'from'=>'弹溜溜官方','create_time'=>  time()),1);
    }
    
//    function ttt(){
//        $this->load->library('my_getui');
//        
//        $push = array();
//        $push['type'] = 1;
//        $push['title'] = "奖励通知123";
//        $push['content'] = "您收到一条奖励通知";
//        $push['message_id'] = 16;
//        $Push = new My_getui();
//        $Push->pushMessageToApp($push);
//    }
//    
    function tttt(){
        $this->load->library('my_getui');
        $push = array();
        $push['type'] = 1;
        $push['title'] = "奖励通知123";
        $push['content'] = "您收到一条活动通知";
        $push['message_id'] = 16;
        $Push = new My_getui();
        $resp = $Push->pushMessageToList(array("20160451","2026616","156454546","2026001"),$push);
        p($resp);
    }
    
    
    function zhima(){
        $url = "https://zmcustprod.zmxy.com.cn/auth/index.htm?zhima_exterface_invoke_assign_target=0a6edc301517554276416835453605&zhima_exterface_invoke_assig_sign=QCMGLKQh1vYSwtVzFMf27IS-zJlcXHDZUNZSxHhGooea-07gcP_Y9bVaruLfWqFlOUaGYwv973hQJ43NLsFX8WNNCCQrtwzWHikv3Sn4lCxGVaiSna8roEWkHxcZvVjt7ZZC1D7XyKmbCNA1maxFFsi9_ZZi3ajKNP2zCE9sBD0";
        $html = file_get_contents("compress.zlib://".$url);
        echo $html;
    }
    
    
}
